package gui.browse;

import gui.util.GenericTableModel;
import gui.util.JFrameCadastro;

import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;

import controller.ControllerBrowse;

public class Browse extends JFrame {
	private static final long serialVersionUID = 1L;

	Browse thisFrame = this;
	private JTable table;
	private BrowseTableModel model;
	private BrowseListener bListener;

	public void addBrowseListener(BrowseListener bListener) {
		this.bListener = bListener;
	}

	public Browse(ControllerBrowse cb) {
		
		ArrayList<HashMap<String, Object>> dados = null;
		if (dados == null || dados.isEmpty()) {
			dados = new ArrayList<HashMap<String, Object>>();
		}
		try {
			dados = cb.buscarDados();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		getModel().setData(dados);
	
		this.add(new JScrollPane(getTable(),
				JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
				JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED));
		this.setSize(300, 250);
		this.setLocationRelativeTo(null);
		this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
		this.setResizable(false);
		this.setVisible(true);
	}

	public JTable getTable() {
		if (table == null) {
			table = new JTable(getModel());
			table.addMouseListener(new MouseListener() {

				@Override
				public void mouseReleased(MouseEvent arg0) {
					// TODO Auto-generated method stub

				}

				@Override
				public void mousePressed(MouseEvent arg0) {
					// TODO Auto-generated method stub

				}

				@Override
				public void mouseExited(MouseEvent arg0) {
					// TODO Auto-generated method stub

				}

				@Override
				public void mouseEntered(MouseEvent arg0) {
					// TODO Auto-generated method stub

				}

				@Override
				public void mouseClicked(MouseEvent arg0) {
					if (bListener != null)
						bListener.linhaClicada(getModel().getDados().get(
								table.getSelectedRow()));
					thisFrame.dispose();

				}
			});
		}
		return table;
	}

	public BrowseTableModel getModel() {
		if (model == null) {
			model = new BrowseTableModel();
		}
		return model;
	}

	public class BrowseTableModel extends
			GenericTableModel<HashMap<String, Object>> {
		private static final long serialVersionUID = 1L;

		private ArrayList<HashMap<String, Object>> dados;

		public ArrayList<HashMap<String, Object>> getDados() {
			return dados;
		}

		public BrowseTableModel() {
			dados = new ArrayList<HashMap<String, Object>>();
		}

		public BrowseTableModel(List<HashMap<String, Object>> dados) {
			this();
			setData(dados);
		}

		@Override
		public String getColumnName(int col) {
			return getColumnNames()[col];
		}

		public String[] getColumnNames() {
			return new String[] { "Codigo", "Descricao" };
		}

		@Override
		public void setData(List<HashMap<String, Object>> dado) {
			this.dados.clear();
			this.dados.addAll(dado);
			super.fireTableDataChanged();
		}

		@Override
		public HashMap<String, Object> getValueAt(int row) {
			return dados.get(row);
		}

		@Override
		public int indexOf(HashMap<String, Object> dado) {
			return dados.indexOf(dado);
		}

		@Override
		public void clear() {
			this.dados.clear();
			super.fireTableDataChanged();
		}

		@Override
		public void remove(HashMap<String, Object> dado) {
			dados.remove(dado);
			super.fireTableDataChanged();
		}

		@Override
		public void add(HashMap<String, Object> dado) {
			dados.add(0, dado);
			super.fireTableDataChanged();
		}

		@Override
		public boolean contains(HashMap<String, Object> dado) {
			return dados.contains(dado);
		}

		public int getRowCount() {
			return dados.size();
		}

		public int getColumnCount() {
			return getColumnNames().length;
		}

		public Object getValueAt(int rowIndex, int columnIndex) {
			HashMap<String, Object> map = dados.get(rowIndex);
			switch (columnIndex) {
			case 0:
				return map.get("codigo");
			case 1:
				return map.get("descricao");
			}
			return null;
		}

		@Override
		public List<HashMap<String, Object>> list() {
			return dados;
		}

		@Override
		public void updateItem(int idx, HashMap<String, Object> dado) {
			dados.set(idx, dado);
			super.fireTableDataChanged();
		}
	}
}
